# -*- coding:utf-8 -*-

"""
version: 1.00.01
change: 2010-09-07 增加标志位，用于停止获取列表中元素
"""

import thread

#互斥列表定义
class MutexList:
    def __init__(self):
        """
        初始化
        """
        self.object_list = []
        self.mutex_lock = thread.allocate_lock()
        self.flag = True
        
    def add(self, object):
        """
        添加一个元素到列表中
        @param object: 新元素
        """
        self.mutex_lock.acquire()
        self.object_list.append(object)
        self.mutex_lock.release()

    def extend(self, list):
        self.mutex_lock.acquire()
        self.object_list.extend(list)
        self.mutex_lock.release()
        
    def set_flag(self, flag):
        """
        设置标志位
        @param flag: 标志位值，True或False 
        """
        self.mutex_lock.acquire()
        self.flag = flag
        self.mutex_lock.release()
        
    def get_flag(self):
        """
        获取标识位
        @return: 标识为值，True或False 
        """
        flag = None
        self.mutex_lock.acquire()
        flag = self.flag
        self.mutex_lock.release()
        return flag
        
    def get(self):
        """
        获取列表第一个元素，并且从列表中删除这个元素
        @return: 第一个元素
        """
        object = None
        if self.flag:
            self.mutex_lock.acquire()
            if len(self.object_list):
                object = self.object_list.pop(0)
            self.mutex_lock.release()
        return object 
        
    def del_item(self,itemname):
        """
        从列表里面删除掉某个元素
        @return: 返回删除的元素名称
        """
        if self.flag:
            self.mutex_lock.acquire()
            try:
                if len(self.object_list):
                    if itemname in self.object_list:
                        self.object_list.remove(itemname)
            except:
                pass
            self.mutex_lock.release()
        return itemname 
    
    def get_force(self):
        """
        获取列表最后一个元素，忽略flag标志
        @return: 最后一个元素
        """
        object = None
        self.mutex_lock.acquire()
        if len(self.object_list):
            object = self.object_list.pop()
        self.mutex_lock.release()
        return object 
    
    def get_all(self):
        """
        获取列表中所有元素，并且清空列表
        """
        object_list = []
        if self.flag:
            self.mutex_lock.acquire()
            object_list = self.object_list
            self.object_list = []
            self.mutex_lock.release()
        return object_list
        
    def size(self):
        """
        获取列表中元素个数
        @return: 元素个数
        """
        object_count = None
        self.mutex_lock.acquire()
        object_count = len(self.object_list)
        self.mutex_lock.release()
        return object_count
    
    def copy(self):
        """
        拷贝列表中所有元素到一个新的列表
        @return: 所有元素的拷贝
        """
        object_list = []
        self.mutex_lock.acquire()
        object_list.extend(self.object_list)
        self.mutex_lock.release()
        return object_list 
    
    def _show(self):
        """
        输出列表中所有内容，元素被强制转换成string数据类型       
        """
        print ", ".join([str(object) for object in self.object_list])

#单元测试
import unittest
import time
  
class MutexListTestCase(unittest.TestCase):
    def setUp(self):
        self.mutex_list = MutexList()
    
    def tearDown(self):
        pass
    
    def test_func(self):
        self.mutex_list.add(1)
        self.mutex_list.add(2)
        self.mutex_list.add(3)
        self.mutex_list._show()
        print "size:", self.mutex_list.size()
        print "copy:", self.mutex_list.copy()
        print "get:", self.mutex_list.get()
        self.mutex_list._show()
        print "get_all:", self.mutex_list.get_all()
        self.mutex_list._show()
    
    def test_thread(self):
        thread.start_new_thread(self.producer, ())
        thread.start_new_thread(self.customer, ())
        thread.start_new_thread(self.spectator, ())
        time.sleep(40)
        
    def spectator(self):
        flag = False
        while True:
            self.mutex_list.set_flag(flag)
            print self.mutex_list._show()
            flag = not flag
            time.sleep(10)
            
    def producer(self):
        count = 1;
        while(True):
            self.mutex_list.add(count)
            count += 1
            time.sleep(1.5)
            
    def customer(self):
        while(True):
            object = self.mutex_list.get()
            if object:
                print object
            else:
                print "None"
            time.sleep(2)
                
if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(MutexListTestCase("test_func"))
    suite.addTest(MutexListTestCase("test_thread"))
    
    runner = unittest.TextTestRunner()
    runner.run(suite)
